package personal.xjl._23java2studentdemo.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import personal.xjl._23java2studentdemo.model.Course;
import personal.xjl._23java2studentdemo.model.Student;
import personal.xjl._23java2studentdemo.model.Teacher;
import personal.xjl._23java2studentdemo.service.CourseService;
import personal.xjl._23java2studentdemo.service.impl.CourseServiceImpl;

import java.util.List;
import java.util.Scanner;


@Controller
@RequestMapping("/Course")
public class CourseController {
    @Autowired
    CourseService courseService;

    @Autowired
    CourseServiceImpl courseServiceImpl;

    @RequestMapping("list")
    public String list(Model model) {
        List<Course> courses=courseService.findAll();
        model.addAttribute("courses",courses);
        return "listCourses";
    }

    @RequestMapping("toPage")
    public String toPage(String page){
        return page;
    }

    //删除课程
    @RequestMapping("remove")
    public String remove(int id){
        courseService.remove(id);
        //重定向到List方法，刷新数据，重新select，返回给用户看
        return "redirect:/Course/list";
    }

    //新增课程
    @RequestMapping("add")
    public String add(Course course){
        //调用service的增加方法，返回到显示页面
        courseService.add(course);
        return "redirect:/Course/list";
    }

    //修改
    @RequestMapping("edit")
    public String edit(Course course){
        courseService.edit(course);
        return "redirect:/Course/list";
    }
    @RequestMapping("toEdit")
    public String toEdit(int id, Model model){
        Course course = courseService.findOneById(id);
        model.addAttribute("course", course);
        return "editCourse";
    }

    //分页显示
    @RequestMapping("list/{currPage}/{pageSize}")
    public String list(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize){
        if(currPage<1) currPage=1;
        if(pageSize<1) pageSize=5;
        List<Course> courses = courseService.findByPage(currPage, pageSize);
        model.addAttribute("courses", courses);
        return "listCourses";
    }

}
